package com.sfeir.poc.portal.gui.client.widgets;

import com.google.gwt.user.client.ui.HTMLPanel;
import com.sfeir.poc.portal.gui.client.objects.News;
import com.sfeir.poc.portal.gui.client.objects.WidgetDescription;

/**
 * Quite similar to RssWidget but specific to spreadsheets feeds
 * @author bostoen.v
 *
 */
public class NewsWidget extends AbstractWidget {

	private HTMLPanel newsPanel = null;

	public NewsWidget(WidgetDescription widgetDescription){
		super();
		newsPanel = new HTMLPanel(initContent(widgetDescription));
		newsPanel.setWidth("100%");
		newsPanel.setHeight("100%");
		widgetContainer.add(newsPanel);
	}

	private String initContent(WidgetDescription widgetDescription){
		String html = "<table width=\"100%\">";
		int end = widgetDescription.getNews().size();
		if (end > new Integer(widgetDescription.getArgs().get(0))) {
			end = new Integer(widgetDescription.getArgs().get(0));
		}
		News aNews = null;
		for (int i = 0; i < end; i++) {
			aNews = (News) widgetDescription.getNews().get(i);
			html += "<tr><td class=\"news\"><small style=\"color: rgb(170, 170, 170);\">" + aNews.getDate() + "</small></td><td class=\"news\"><a href=\"" + aNews.getUrl() + "\" target=\"_blank\">" + aNews.getTitre() + "</a></td></tr>";
		}
		html += "</table>";
		return html;
	}
}
